<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资产类型" prop="assetType">
        <el-input v-model="queryParams.assetType" placeholder="请输入类型" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="单据日期" prop="cardTime">
        <el-date-picker
            v-model="queryParams.cardTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
        />
      </el-form-item>
      <el-form-item label="资产编码" prop="assetCode">
        <el-input v-model="queryParams.assetCode" placeholder="请输入编码" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="资产名称" prop="assetName">
        <el-input v-model="queryParams.assetName" placeholder="请输入名称" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="单据号" prop="assetNumber">
        <el-input v-model="queryParams.assetNumber" placeholder="请输入单据号" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Switch" :disabled="multiple" @click="makeVoucher()">
          生成卡片
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="gridData" show-overflow-tooltip @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column prop="field0020" width="220px" label="单据号" algin="center"/>
      <el-table-column prop="field0075Name" width="250px" label="所属公司" algin="center"/>
      <el-table-column prop="field0040Name" label="资产类型" algin="center"/>
      <el-table-column prop="field0024" label="资产编码" algin="center"/>
      <el-table-column prop="field0025" label="资产名称" algin="center"/>
      <el-table-column prop="field0026" width="80px" label="规格型号" algin="center"/>
      <el-table-column prop="field0027" width="80px" label="购入价格" algin="center"/>
      <el-table-column prop="field0028" label="入库日期" algin="center"/>
      <el-table-column prop="documentType" width="85px" label="单据类型" algin="center"/>
      <el-table-column prop="field0029" label="存货地点" algin="center"/>
      <el-table-column prop="field0035" width="150px" label="使用部门" algin="center"/>
    </el-table>
    <!--    <el-pagination :total="total" v-model:current-page="listQuery.page" v-model:page-size="listQuery.pageSize"-->
    <!--                   @current-change="getList"/>-->

    <el-dialog title="卡片制作" v-model="open" width="80%">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="净残值率" prop="scraValue">
              <el-input v-model="form.scraValue" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始使用日期" prop="begnDate">
              <el-date-picker
                  v-model="form.begnDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择日期"
                  style="width:100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="折旧方法" prop="depreciation">
              <el-select v-model="form.depreciation" filterable placeholder="请选择折旧方法" style="width:100%" clearable>
                <el-option v-for="item in depresOptions" :key="item.cateName" :label="item.cateName"
                           :value="item.cateName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="增加方式" prop="fkAddreducestyle">
              <el-select v-model="form.fkAddreducestyle" filterable placeholder="请选择增加方式" style="width:100%" clearable>
                <el-option v-for="item in AddOptions" :key="item.cateName" :label="item.cateName"
                           :value="item.cateName"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="资产类别" prop="assetsClass">
              <el-select v-model="form.assetsClass" filterable placeholder="请选择资产类别" style="width:100%">
                <el-option v-for="item in assetsOptions" :key="item.cateName" :label="item.cateName"
                           :value="item.cateName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用月限" prop="playMothe">
              <el-input v-model="form.playMothe" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="管理部门" prop="section">
              <el-select v-model="form.section" filterable placeholder="请选择管理部门" style="width:100%" clearable>
                <el-option v-for="item in deptOptions" :key="item.cateName" :label="item.cateName"
                           :value="item.cateName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用部门" prop="playDapartment">
              <el-select v-model="form.playDapartment" filterable placeholder="请选择使用部门" style="width:100%" clearable>
                <el-option v-for="item in deptOptions" :key="item.cateName" :label="item.cateName"
                           :value="item.cateName"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="录入人" prop="personnel">
              <el-select v-model="form.personnel" filterable placeholder="请选择人员" style="width:100%">
                <el-option v-for="item in personOptions" :key="item.userName" :label="item.userName"
                           :value="item.userName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
        <el-table :data="selectionList" @cell-mouse-enter="handleCellEnter" @cell-mouse-leave="handleCellLeave">
          <el-table-column prop="field0020" label="单据号" algin="center"/>
          <el-table-column prop="field0024" label="资产编码" algin="center"/>
          <el-table-column prop="field0025" label="资产名称" width="82px" algin="center"/>
          <el-table-column prop="field0026" label="规格型号" width="80px" algin="center"/>
          <el-table-column prop="field0027" label="购入价格" width="80px" algin="center"/>
          <el-table-column prop="field0028" label="入库日期" algin="center"/>
<!--          将存货地点改为可编辑的-->
          <el-table-column label="存货地点" prop="field0029" width="200px">
            <template v-slot="scope">
              <el-input v-if="scope.row.isEdit" class="item" v-model="scope.row.field0029" placeholder="请输入地点"></el-input>
              <div v-else class="txt">{{scope.row.field0029}}</div>
            </template>
          </el-table-column>
          <el-table-column label="原值" prop="field0036">
            <template v-slot="scope">
              <el-input v-if="scope.row.isEdit" class="item" v-model="scope.row.field0036" placeholder="请输入原值"></el-input>
              <div v-else class="txt">{{scope.row.field0036}}</div>
            </template>
          </el-table-column>
          <el-table-column prop="field0075Name" label="摘要" algin="center"/>
        </el-table>
      </el-form>
      <template v-slot:footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">生 成</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import request from "@/utils/request";
import {ElMessage} from "element-plus";
import axios from "axios";

export default {
  name: "ImportLog",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      selectionList: [],
      gridData: [],
      listQuery: {
        page: 1,
        pageSize: 15,
      },
      queryParams: {
        assetType: null,
        cardTime: null,
        assetCode: null,
        assetName: null,
        assetNumber: null,
      },
      total: 0,
      typeOptions: [],
      assetsOptions: [],
      depresOptions: [],
      AddOptions: [],
      deptOptions: [],
      personOptions: [],
      open: false,
      // 表单参数
      form: {},
      cards: [],
      rules: {
        fkAddreducestyle: [{required: true, message: "请选择增加方式", trigger: "blur"}],
        scraValue: [
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,10})?$/,
            message: "请输入合法的数字",
            trigger: "change"
          }],
        assetCode: [{required: true, message: "请选择资产编码", trigger: "blur"}],
        assetName: [{required: true, message: "请输入资产名称", trigger: "blur"}],
        begndate: [{required: true, message: "请选择开始使用日期", trigger: "blur"}],
        personnel: [{required: true, message: "请选择录入人员", trigger: "blur"}],
        assetsClass: [{required: true, message: "请选择资产类别", trigger: "blur"}],
        pkCorp: [{required: true, message: "所属公司不能为空，请在OA里面添加", trigger: "blur"}],
        playDapartment: [{required: true, message: "请选择使用部门", trigger: "blur"}],
        section: [{required: true, message: "请选择管理部门", trigger: "blur"}],
        depreciation: [{required: true, message: "请选择折旧方式", trigger: "blur"}],
        currmoney: [
          {type: 'number', message: '请输入正确数据', trigger: 'blur'}
        ],
        playMothe: [
          {required: true, message: "请输入使用月限", trigger: "blur"},
          {
            pattern: /^[0-9]+$/,
            message: '只能输入数字',
            trigger: 'blur'
          }
        ]
      },
    }
  },
  watch: {},
  computed: {
    timeDefault() {
      var date = new Date();
      var s1 = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate());
      return s1;
    }
  },
  //页面加载时候，在mounted中进行赋值
  mounted() {
    // 初始化查询，默认为前一天
    this.form.begnDate = this.timeDefault;
  },
  created() {
    this.getList();
    this.getChargeType();
  },
  methods: {
    /** 鼠标移入cell */
    handleCellEnter (row) {
      row.isEdit = true
    },
    /** 鼠标移出cell */
    handleCellLeave (row) {
      row.isEdit = false
    },
    getChargeType() {
      request.get('/formmain0125/getChargeType').then(res => {
        this.typeOptions = res.data;
      });
    },
    getList() {
      this.loading = true;
      let data = {
        ...this.listQuery,
        ...this.queryParams
      }
      request.post('/formmain0112/postBy', data).then(res => {
        this.loading = false;
        this.gridData = res.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.listQuery.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.listQuery.page = 1;
      this.queryParams.assetType = null;
      this.queryParams.cardTime = null;
      this.queryParams.assetCode = null;
      this.queryParams.assetName = null;
      this.queryParams.assetNumber = null;
      this.form = {
        assetCode: null,
        assetName: null,
        assetsClass: null,
        begnDate: null,
        cardCode: null,
        currmoney: null,
        depreciation: null,
        fkAddreducestyle: null,
        formmainId: null,
        pkCorp: null,
        playDapartment: null,
        playMothe: null,
        position: null,
        price: null,
        scraValue: null,
        section: null,
        spec: null,
        storageTime: null,
        personnel: null,
      }
      this.handleQuery();
    },
    makeVoucher() {
      request.get('/faCategory/getList').then(res => {
        this.assetsOptions = res.data
        this.assetsOptions.forEach(function (item) {
          item.cateName = item.cateCode + '-' + item.cateName
        })
      });
      request.get('/faDepmethod/getList').then(res => {
        this.depresOptions = res.data
        this.depresOptions.forEach(function (item) {
          item.cateName = item.depmethodCode + '-' + item.depmethodName
        })
      })
      request.get('/faAddreducestyle/getList').then(res => {
        this.AddOptions = res.data
        this.AddOptions.forEach(function (item) {
          item.cateName = item.styleCode + '-' + item.styleName
        })
      })
      request.get('/bdDeptdoc/getListAll').then(res => {
        this.deptOptions = res.data
        this.deptOptions.forEach(function (item) {
          item.cateName = item.deptcode + '-' + item.deptname
        })
      })
      request.get('/smUser/getList').then(res => {
        this.personOptions = res.data
      })
      // this.form.formmainId = this.selectionList[0].formmainId
      // this.form.assetCode = this.selectionList[0].field0024
      // this.form.assetName = this.selectionList[0].field0025
      // this.form.spec = this.selectionList[0].field0026
      // this.form.position = this.selectionList[0].field0029
      // this.form.cardCode = this.selectionList[0].field0020
      // this.form.pkCorp = this.selectionList[0].field0075Name
      // this.form.price = this.selectionList[0].field0036
      // this.form.currmoney = this.selectionList[0].field0027
      // this.form.storageTime = this.selectionList[0].field0028
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.resetQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.begnDate == null) {
            ElMessage({
              showClose: true,
              message: '请选择开始使用日期',
              type: 'error',
            });
            return
          }
          this.selectionList.forEach(item => {
            let detail = {
              formmainId: null,
              pkCorp: null,
              cardCode: null,
              assetCode: null,
              assetName: null,
              spec: null,
              price: null,
              storageTime: null,
              position: null,
              assetsClass: null,
              playMothe: null,
              fkAddreducestyle: null,
              depreciation: null,
              section: null,
              playDapartment: null,
              currmoney: null,
              begnDate: null,
              scraValue: null,
              personnel: null
            }
            detail.formmainId = item.formmainId
            detail.pkCorp = item.field0075Name
            detail.cardCode = item.field0020
            detail.assetCode = item.field0024
            detail.assetName = item.field0025
            detail.spec = item.field0026
            detail.price = item.field0036
            detail.storageTime = item.field0028
            detail.position = item.field0029
            detail.assetsClass = this.form.assetsClass
            detail.playMothe = this.form.playMothe
            detail.fkAddreducestyle = this.form.fkAddreducestyle
            detail.depreciation = this.form.depreciation
            detail.section = this.form.section
            detail.playDapartment = this.form.playDapartment
            detail.currmoney = item.field0027
            detail.begnDate = this.form.begnDate
            detail.scraValue = this.form.scraValue
            detail.personnel = this.form.personnel
            this.cards.push(detail)
          })
          axios({
            url: '/api/faCard/save',
            method: 'post',
            headers: {'Content-Type': 'application/json'},
            data: this.cards
          }).then(res => {
            if (res.data.success) {
              ElMessage({
                showClose: true,
                message: res.data.data,
                type: 'success',
              })
            } else {
              ElMessage({
                showClose: true,
                message: res.data.data,
                type: 'error',
              })
            }
            this.open = false;
            this.resetQuery();
          }).catch(error => {
            console.log(error)
            this.open = false;
            this.resetQuery();
            ElMessage({
              showClose: true,
              message: '生成失败！请联系管理员',
              type: 'error',
            });
          })
          this.open = false;
        }
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectionList = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
}
</script>

<style scoped>

.mb8 {
  margin-bottom: 20px;
}

.app-container {
  margin: 0;
  height: 96%;
  padding: 30px 10px 10px;
  background-color: #FFFFFF;
}

.item{
  width: 180px;
  /* 调整elementUI中样式 如果不需要调整请忽略 */
    .el-input__inner{
    height: 24px!important;
  }
}
.txt{
  line-height: 24px;
  padding: 0 9px;
  box-sizing: border-box;
}

</style>